package cn.xiaoyu.su.controller;


import cn.xiaoyu.su.controller.request.ShortLinkAddRequest;
import cn.xiaoyu.su.controller.request.ShortLinkDelRequest;
import cn.xiaoyu.su.controller.request.ShortLinkPageRequest;
import cn.xiaoyu.su.controller.request.ShortLinkUpdateRequest;
import cn.xiaoyu.su.service.ShortLinkService;
import cn.xiaoyu.su.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ⼆当家⼩D
 * @since 2022-12-03
 */
@RestController
@RequestMapping("/api/short_link/v1")
public class ShortLinkController {
    @Autowired
    private ShortLinkService shortLinkService;

    @PostMapping("add")
    public JsonData createShortLink(@RequestBody ShortLinkAddRequest shortLinkAddRequest) {

        JsonData jsonData = shortLinkService.createShortLink(shortLinkAddRequest);

        return jsonData;
    }

    /**
     * 分页查询短链
     * @param pageRequest
     * @return
     */
    @RequestMapping("page")
    public JsonData pageByGroupId(@RequestBody ShortLinkPageRequest pageRequest) {

        Map<String,Object> result = shortLinkService.pageByGroupId(pageRequest);

        return JsonData.buildSuccess(result);
    }

    /**
     * 删除短链
     * @param request
     * @return
     */
    @PostMapping("del")
    public JsonData del(@RequestBody ShortLinkDelRequest request) {

        JsonData jsonData = shortLinkService.del(request);
        return jsonData;
    }

    /**
     * 更新短链
     * @param request
     * @return
     */
    @PostMapping("update")
    public JsonData update(@RequestBody ShortLinkUpdateRequest request) {

        JsonData jsonData = shortLinkService.update(request);
        return jsonData;
    }
}

